﻿using DLL.NET70.Infrastructure.Prism;
using DLL.NET70.Infrastructure.Prism.MVVM;
using DLL.Standard.Infrastructure.Log;

using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;

using System;
using System.Collections.Generic;
using System.Linq;

namespace Module.ExternalDevice.ViewModels
{
    public class PUC_ExternalDeviceShellViewModel : CMVVMBase, IRegionMemberLifetime
    {
        public bool KeepAlive
        {
            get
            {
                //return false;//这个的作用是，只要离开当前切面，就注销掉当前页面
                return false;//这个的作用是，只要离开当前切面，就注销掉当前页面
            }
        }

        private IRegionManager _regionManager;

        public PUC_ExternalDeviceShellViewModel(IRegionManager regionManager,
            IEventAggregator eventAggregator,
            IApplicationCommands applicationCommands) : base(regionManager, eventAggregator, applicationCommands)
        {
            #region navigate

            _regionManager = regionManager;
            NavigateCommand = new DelegateCommand<string>(Navigate);

            #endregion navigate
        }

        #region windows load

        public override void Loaded()
        {
            //NavigateToShowAll();
            try
            {
                string navigatePath = "";
                string region = "";

                //region = "SingleDJRegion";
                //navigatePath = "PUC_OneDjTest";
                //Navigate(region, navigatePath);

                //region = "MultyDJRegion";
                //navigatePath = "PUC_DJ_ZL_MS";
                //Navigate(region, navigatePath);
                //region = "LEDRegion";
                navigatePath = "McuControlLed";
                Navigate(navigatePath);
                base.Loaded();
            }
            catch (Exception e)
            {
                LogHelper.Warn("PUC_ExternalDeviceShell:Loaded 出现异常！" + e.Message);
            }
        }

        public override void UnLoaded()
        {
            try
            {
                //_regionManager.Regions.Remove("ExternalDeviceRegion");
                base.UnLoaded();
            }
            catch (Exception e)
            {
                LogHelper.Warn("PUC_ExternalDeviceShell:UnLoaded 出现异常！" + e.Message);
            }
        }

        #endregion windows load

        public DelegateCommand<string> NavigateCommand { get; private set; }

        private void Navigate(string viewName)
        {
            Navigate("ExternalDeviceRegion", viewName);
        }

        private void Navigate(string region, string navigatePath)
        {
            if (navigatePath != null)
            {
                _regionManager.RequestNavigate(region, navigatePath);
            }
        }
    }
}